<template>
    <el-dialog
        :before-close="resetForm"
        :close-on-click-modal="false"
        :visible.sync="visible"
        append-to-body
        title="编辑目录类型信息"
        width="560px"
    >
        <div class="edit_page_div">
            <el-form ref="form" :model="form" :rules="rules" :show-message="false" label-width="100px">
                <el-form-item label="父级类型" prop="parentsId">
                    <select-option v-model="form.parentsId" select-width="400px" :list="typeList" :disabled="form.id"></select-option>
                </el-form-item>
                <el-form-item label="目录编号" prop="typeCode">
                    <el-input v-model="form.typeCode" style="width: 400px;" placeholder="系统生成" disabled></el-input>
                </el-form-item>
                <el-form-item label="目录名称" prop="typeName">
                    <el-input v-model="form.typeName" style="width: 400px;"></el-input>
                </el-form-item>
                <el-form-item label="目录说明" prop="typeExplain">
                    <el-input v-model="form.typeExplain" style="width: 400px;" type="textarea" rows="3"></el-input>
                </el-form-item>
                <el-form-item label="目录顺序" prop="orderNum">
                    <el-input v-model="form.orderNum" style="width: 400px;"></el-input>
                </el-form-item>
                <el-form-item label="备注" prop="remarks">
                    <el-input v-model="form.remarks" style="width: 400px;" type="textarea" rows="3"></el-input>
                </el-form-item>
                <el-form-item label="是否发布" prop="releaseFlag">
                    <radios-option v-model="form.releaseFlag"></radios-option>
                </el-form-item>
            </el-form>
            <div class="edit_button">
                <el-button v-loading.fullscreen.lock="loading" type="primary" @click="submit">保 存</el-button>
                <el-button type="primary" @click="resetForm">取 消</el-button>
            </div>
        </div>
    </el-dialog>
</template>

<script>
import SelectOption from "@/components/select/selectOption.vue";
import RadiosOption from "@/components/radio/radiosOption.vue";

export default {
    name: "newsArticleTypeEdit",
    components: {RadiosOption, SelectOption},
    props: {
        visible: Boolean,
        id: String,
    },
    data() {
        return {
            loading: false,

            form: {
                parentsId: '',
                typeCode: '',
                typeName: '',
                typeExplain: '',
                orderNum: 1,
                remarks: '',
                releaseFlag: 0,
            },
            rules: {
                parentsId: [{required: true, message: '不能为空', trigger: 'change'}],
                typeName: [{required: true, message: '不能为空', trigger: 'change'}],
                typeExplain: [{required: true, message: '不能为空', trigger: 'change'}],
                orderNum: [{required: true, message: '不能为空', trigger: 'change'}],
            },

            typeList: [],
        }
    },
    watch: {
        visible(newVal) {
            if (newVal) {
                this.getTypeList();
            }
        }
    },
    methods: {
        getTypeList() {
            this.loading = true;
            this.$axios.post("/CHILD_SYSTEM/newsArticleType/getAllList").then(res => {
                this.typeList = this.$common.getSelectList(res.data, "typeName", "id", true);
                if (this.id) {
                    this.getData();
                }
            }).finally(() => {
                this.loading = false;
            })
        },
        getData() {
            this.loading = true;
            this.$axios.get("/CHILD_SYSTEM/newsArticleType/getOneEntity/" + this.id).then(res => {
                this.form = res.data;
            }).finally(() => {
                this.loading = false;
            })
        },
        submit() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.loading = true;
                    this.$axios.post("/CHILD_SYSTEM/newsArticleType/" + (this.id ? "update" : "insert"), this.form).then(() => {
                        this.resetForm();
                        this.$message.success(res.message);
                    }).finally(() => {
                        this.loading = false;
                    })
                } else {
                    this.$message.error('表单填写有误');
                    return false;
                }
            });
        },
        resetForm() {
            this.$refs.form.resetFields();
            this.form = this.$options.data().form;
            this.$emit('update:visible', false);
            this.$emit('refreshList');
        },
    }
}
</script>

<style scoped>

</style>